package com.maila.xiaoaoschool.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.cjj.MaterialRefreshLayout;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.MessageAdapter;
import com.maila.xiaoaoschool.widget.xlistview.XListView;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 系统消息
 * Created by Administrator on 2017/10/19.
 */

public class MessageActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.lv_fujin)
    XListView lvFujin;
    @BindView(R.id.hot_refresh)
    MaterialRefreshLayout hotRefresh;
    MessageAdapter madpter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        madpter=new MessageAdapter(this);
        lvFujin.setAdapter(madpter);
        lvFujin.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                new AsyncTask<Void, Void, Void>() {
                    protected Void doInBackground(Void... params) {
                        try {
                            Thread.sleep(3000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        lvFujin.setPullLoadEnable(true);
                        onLoad(lvFujin);
                    }
                }.execute(null, null, null);
            }

            @Override
            public void onLoadMore() {
                new AsyncTask<Void, Void, Void>() {
                    protected Void doInBackground(Void... params) {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        lvFujin.setPullLoadEnable(true);
                        onLoad(lvFujin);
                    }
                }.execute(null, null, null);
            }
        });
    }

    private void onLoad(XListView lvFujin) {
        lvFujin.stopRefresh();
        lvFujin.stopLoadMore();
        lvFujin.setRefreshTime(new Date().toLocaleString());
    }

    @OnClick(R.id.white_return_iv)
    public void onViewClicked() {
        finish();
    }
}
